#include <iostream>
#include <vector>
#include <cmath>

const double PI = 3.141592653589793;

// 生成正弦波作为载体信号
int generate_cover_signal(std::vector<double>& cover, const int size) {
    for (int i = 0; i < size; ++i) {
        cover[i] = sin(2 * PI * 50 * i / size);
    }
    return 0;
}

// 模拟数字调制信号
int simulate_digital_modulation_signal(std::vector<unsigned char>& message, const int size) {
    for (int i = 0; i < size; ++i) {
        message[i] = rand() % 2;
    }
    return 0;
}

// 模拟模拟调制信号
int simulate_analog_modulation_signal(std::vector<double>& message, const int size) {
    for (int i = 0; i < size; ++i) {
        message[i] = (double)(rand() % 100) / 100.0;
    }
    return 0;
}

// 调频
int modulate_digital_frequency(std::vector<double>& cover, const int cover_len, const std::vector<unsigned char>& message, const int msg_len) {
    double kf = 10;
    for (int i = 0; i < cover_len; ++i) {
        int msg_index = i % msg_len;
        cover[i] = sin(2 * PI * 50 * i / cover_len + 2 * PI * kf * message[msg_index] * i / cover_len);
    }
    return 0;
}

// 调幅
int modulate_analog_amplitude(std::vector<double>& cover, const int cover_len, const std::vector<double>& message, const int msg_len) {
    for (int i = 0; i < cover_len; ++i) {
        int msg_index = i % msg_len;
        cover[i] *= (1 + message[msg_index]);
    }
    return 0;
}

// 调相
int modulate_digital_phase(std::vector<double>& cover, const int cover_len, const std::vector<unsigned char>& message, const int msg_len) {
    double kp = 1;
    for (int i = 0; i < cover_len; ++i) {
        int msg_index = i % msg_len;
        cover[i] = sin(2 * PI * 50 * i / cover_len + 2 * PI * kp * message[msg_index]);
    }
    return 0;
}

int main() {
    const int size = 100;
    std::vector<double> cover_signal(size);
    std::vector<unsigned char> digital_message(size);
    std::vector<double> analog_message(size);

    // 生成信号
    generate_cover_signal(cover_signal, size);
    simulate_digital_modulation_signal(digital_message, size);
    simulate_analog_modulation_signal(analog_message, size);

    // 调制
    modulate_digital_frequency(cover_signal, size, digital_message, size);
    modulate_analog_amplitude(cover_signal, size, analog_message, size);
    modulate_digital_phase(cover_signal, size, digital_message, size);

    // 输出调制后的部分信号值
    std::cout << "Modulated cover signal (first 10 values): ";
    for (int i = 0; i < 10; ++i) {
        std::cout << cover_signal[i] << " ";
    }
    std::cout << std::endl;

    return 0;
}